package com.os.osframe.scaffold.create;

import com.os.osframe.scaffold.domain.ScaffoldXmlField;
import com.os.osframe.scaffold.domain.ScaffoldXmlTable;
import com.os.osframe.scaffold.util.PropertyConfigurerLoader;

import javax.annotation.Resource;
import java.io.File;
import java.util.List;

/**
 * Created by wangdc on 2015-4-27.
 */
public class CreateJspCode extends BaseCreate implements ICreateCode {
    public CreateJspCode(){

    }
    public CreateJspCode(ScaffoldXmlTable scaffoldXmlTable){
        super(scaffoldXmlTable);
    }
    public void create(String packages)throws Exception{
        System.out.println("=====开始生成jsp页面=====");
        //String path = replaceStr(new File("").getAbsolutePath(), "\\", "/");//获取项目的根路径，这里获取到的是softdp的路径
        String basePath = (String) getPropertyConfigurer().getProperty("osframe.file.path");//获取属性配置的服务器路径
        if (basePath.substring(basePath.length() - 1).equals("/")) {
            basePath = basePath.substring(basePath.length() - 1);
        }
        //System.out.println(path);//E:/java/intelijwork/softdp
        String targetFilePath=basePath;
        targetFilePath+="/auto/code/"+packages.replaceAll("\\.","/")+"/jsp";
        //System.out.println(targetFilePath);
        String tempPath = replaceStr(new File(this.getClass().getResource("").getPath()).getAbsolutePath(), "\\", "/");//获取当前类所在路径
        if(tempPath.indexOf("create")>-1){
            tempPath=tempPath.substring(0,tempPath.length()-7);
        }
        tempPath+="/jsp";
        //System.out.println(tempPath);
        //xmlObjList= ReadXmlObj.getDataInfoByModule(module, packages);//获取列表对象
        createFile(tempPath,targetFilePath);
    }

    /**
     *
     * @param srcText
     * @return
     * @throws Exception
     */
    @Override
    protected String replaceCode(String srcText) throws Exception{
        if(scaffoldXmlTable==null ||scaffoldXmlTable.getStatus()==null || "false".equals(scaffoldXmlTable.getStatus())){
            return srcText;
        }
        srcText=replaceEdit(srcText,scaffoldXmlTable);
        srcText=replaceView(srcText,scaffoldXmlTable);
        srcText=replaceList(srcText,scaffoldXmlTable);

        return srcText;
    }

    /**
     * 替换编辑页面
     * @param srcText
     * @param scaffoldXmlTable
     * @return
     * @throws Exception
     */
    protected String replaceEdit(String srcText,ScaffoldXmlTable scaffoldXmlTable) throws Exception{
        List<ScaffoldXmlField> scaffoldXmlFieldList =scaffoldXmlTable.getScaffoldXmlFieldList();
        StringBuffer sbEdit=new StringBuffer();

        int colNum=0;//每行2数量
        for(ScaffoldXmlField scaffoldXmlField : scaffoldXmlFieldList){
            if(colNum==0){
                sbEdit.append("\r\n").append("            ").append("<div class=\"form-group pull-in clearfix\">");
            }

            sbEdit.append("\r\n").append("                ").append("<div class=\"col-sm-6\">");
            sbEdit.append("\r\n").append("                    ").append("<label><fmt:message key=\""+scaffoldXmlTable.getJavaName()+"."+ scaffoldXmlField.getJavaFiledId()+"\"/></label>");

            //
            if(scaffoldXmlField.getForeignId()!=null && !"".equals(scaffoldXmlField.getForeignId())){//外键
                if(scaffoldXmlField.getMapping()!=null && "manytomany".equals(scaffoldXmlField.getMapping().toLowerCase())){
                    sbEdit.append("\r\n").append("                    ").append("<input type=\"hidden\" class=\"form-control\" ng-model=\"formData."+scaffoldXmlField.getJavaFiledId()+"."+scaffoldXmlField.getForeignId()+"\"  ng-init=\"formData."+scaffoldXmlField.getJavaFiledId()+"."+scaffoldXmlField.getForeignId()+"='${"+scaffoldXmlTable.getJavaName()+"."+scaffoldXmlField.getJavaFiledId()+"Ids}'\">");
                    sbEdit.append("\r\n").append("                    ").append("<div>");
                    sbEdit.append("\r\n").append("                        ").append("<input type=\"text\" class=\"form-control\" ng-model=\"formData."+scaffoldXmlField.getJavaFiledId()+"."+scaffoldXmlField.getForeignName()+"\"  ng-init=\"formData."+scaffoldXmlField.getJavaFiledId()+"."+scaffoldXmlField.getForeignName()+"='${"+scaffoldXmlTable.getJavaName()+"."+scaffoldXmlField.getJavaFiledId()+"Names}'\" open-dialog-list ng-bean=\"baseDomainService&domain="+scaffoldXmlField.getJavaType()+"&name="+scaffoldXmlField.getForeignName()+"\" ng-value-id=\"formData."+scaffoldXmlField.getJavaFiledId()+"."+scaffoldXmlField.getForeignId()+"\">");
                    sbEdit.append("\r\n").append("                    ").append("</div>");
                }else{
                    sbEdit.append("\r\n").append("                    ").append("<input type=\"hidden\" class=\"form-control\" ng-model=\"formData['"+scaffoldXmlField.getJavaFiledId()+"."+scaffoldXmlField.getForeignId()+"']\"  ng-init=\"formData['"+scaffoldXmlField.getJavaFiledId()+"."+scaffoldXmlField.getForeignId()+"']='${"+scaffoldXmlTable.getJavaName()+"."+scaffoldXmlField.getJavaFiledId()+"."+scaffoldXmlField.getForeignId()+"}'\">");
                    sbEdit.append("\r\n").append("                    ").append("<div>");
                    sbEdit.append("\r\n").append("                        ").append("<input type=\"text\" class=\"form-control\" ng-model=\"formData['"+scaffoldXmlField.getJavaFiledId()+"."+scaffoldXmlField.getForeignName()+"']\"  ng-init=\"formData['"+scaffoldXmlField.getJavaFiledId()+"."+scaffoldXmlField.getForeignName()+"']='${"+scaffoldXmlTable.getJavaName()+"."+scaffoldXmlField.getJavaFiledId()+"."+scaffoldXmlField.getForeignName()+"}'\" open-dialog-list ng-bean=\"baseDomainService&domain="+scaffoldXmlField.getJavaType()+"&name="+scaffoldXmlField.getForeignName()+"\" ng-value-id=\"formData['"+scaffoldXmlField.getJavaFiledId()+"."+scaffoldXmlField.getForeignId()+"']\">");
                    sbEdit.append("\r\n").append("                    ").append("</div>");

                }
            }else if((scaffoldXmlField.getFiledType()!=null && "date".equals(scaffoldXmlField.getFiledType().toLowerCase()))
                    || (scaffoldXmlField.getJavaType()!=null && "date".equals(scaffoldXmlField.getJavaType().toLowerCase())) ){//时间
                sbEdit.append("\r\n").append("                    ").append("<div>");
                sbEdit.append("\r\n").append("                        ").append("<input type=\"text\" class=\"form-control\" ng-model=\"formData."+scaffoldXmlField.getJavaFiledId()+"\" date-time-select date-type=\"dateTime\" ng-init=\"formData."+scaffoldXmlField.getJavaFiledId()+"='${"+scaffoldXmlTable.getJavaName()+"."+scaffoldXmlField.getJavaFiledId()+"}'\">");
                sbEdit.append("\r\n").append("                    ").append("</div>");
            }else{//其他
                sbEdit.append("\r\n").append("                    ").append("<input type=\"text\" class=\"form-control\"  ng-model=\"formData."+scaffoldXmlField.getJavaFiledId()+"\" ng-init=\"formData."+scaffoldXmlField.getJavaFiledId()+"='${"+scaffoldXmlTable.getJavaName()+"."+scaffoldXmlField.getJavaFiledId()+"}'\">");

            }
            //TODO 按钮，下拉框，等待实现


            sbEdit.append("\r\n").append("                ").append("</div>");

            colNum++;
            if(colNum==2){
                sbEdit.append("\r\n").append("            ").append("</div>");
                colNum=0;
            }
        }
        if(colNum==1){
            sbEdit.append("\r\n").append("                ").append("<div class=\"col-sm-6\"></div>");
            sbEdit.append("\r\n").append("            ").append("</div>");
        }

        srcText=replaceStr(srcText,"${simpleJavaName}",scaffoldXmlTable.getJavaName());
        srcText=replaceStr(srcText,"${editJspCode}",sbEdit.toString());
        srcText=replaceStr(srcText,"${jspCodeTitle}","<fmt:message key=\""+scaffoldXmlTable.getJavaName()+".module\"/>");
        return srcText;
    }
    /**
     * 替换查看页面
     * @param srcText
     * @param scaffoldXmlTable
     * @return
     * @throws Exception
     */
    protected String replaceView(String srcText,ScaffoldXmlTable scaffoldXmlTable) throws Exception{
        List<ScaffoldXmlField> scaffoldXmlFieldList =scaffoldXmlTable.getScaffoldXmlFieldList();
        StringBuffer sbEdit=new StringBuffer();
        int colNum=0;//每行数量
        for(ScaffoldXmlField scaffoldXmlField : scaffoldXmlFieldList){

            if(colNum==0){//每行展示2项
                sbEdit.append("\r\n").append("        ").append("<div class=\"form-group pull-in clearfix\">");
            }
            sbEdit.append("\r\n").append("            ").append(" <div class=\"col-sm-6\">");
            sbEdit.append("\r\n").append("                ").append("<label><fmt:message key=\""+scaffoldXmlTable.getJavaName()+"."+ scaffoldXmlField.getJavaFiledId()+"\"/></label>");
            if(scaffoldXmlField.getForeignId()!=null && !"".equals(scaffoldXmlField.getForeignId())){
                if(scaffoldXmlField.getMapping()!=null && "manytomany".equals(scaffoldXmlField.getMapping().toLowerCase())){
                    sbEdit.append("\r\n").append("                ").append("<label class=\"form-control\">${" + scaffoldXmlTable.getJavaName() + "." + scaffoldXmlField.getJavaFiledId() + "Names}</label>");
                }else {
                    sbEdit.append("\r\n").append("                ").append("<label class=\"form-control\">${" + scaffoldXmlTable.getJavaName() + "." + scaffoldXmlField.getJavaFiledId() + "." + scaffoldXmlField.getForeignName() + "}</label>");
                }
             }else{
                sbEdit.append("\r\n").append("                ").append("<label class=\"form-control\">${"+scaffoldXmlTable.getJavaName()+"."+ scaffoldXmlField.getJavaFiledId()+"}</label>");
            }
            //TODO 按钮，下拉框，等待实现
            sbEdit.append("\r\n").append("            ").append("</div>");

            colNum++;
            if(colNum==2){
                sbEdit.append("\r\n").append("            ").append("</div>");
                colNum=0;
            }
        }
        if(colNum==1){
            sbEdit.append("\r\n").append("                ").append("<div class=\"col-sm-6\"></div>");
            sbEdit.append("\r\n").append("            ").append("</div>");
        }
        srcText=replaceStr(srcText,"${simpleJavaName}",scaffoldXmlTable.getJavaName());
        srcText=replaceStr(srcText,"${viewJspCode}",sbEdit.toString());
        srcText=replaceStr(srcText,"${jspCodeTitle}","<fmt:message key=\""+scaffoldXmlTable.getJavaName()+".module\"/>");
        return srcText;
    }
    /**
     * 替换列表页面
     * @param srcText
     * @param scaffoldXmlTable
     * @return
     * @throws Exception
     */
    protected String replaceList(String srcText,ScaffoldXmlTable scaffoldXmlTable) throws Exception{
        List<ScaffoldXmlField> scaffoldXmlFieldList =scaffoldXmlTable.getScaffoldXmlFieldList();
        //替换列表内容
        StringBuffer sbEdit=new StringBuffer();
        StringBuffer sbTitle=new StringBuffer();
        sbEdit.append("\r\n").append("     ").append("<tr>");
        sbEdit.append("\r\n").append("            ").append("<td><label class=\"i-checks m-b-none\"><input type=\"checkbox\" name=\""+scaffoldXmlTable.getJavaName()+"Ids\" ng-checkbox value=\"${"+scaffoldXmlTable.getJavaName()+".pkId}\"><i></i></label></td>");


                sbEdit.append("\r\n").append("            ").append("<td>${status.index+1}</td>");
        for(ScaffoldXmlField scaffoldXmlField : scaffoldXmlFieldList){
//            sbEdit.append("\r\n").append("            ").append("<td>${"+scaffoldXmlTable.getJavaName()+"."+ scaffoldXmlField.getJavaFiledId()+"} </td>");
            if(scaffoldXmlField.getForeignId()!=null && !"".equals(scaffoldXmlField.getForeignId())){
                if(scaffoldXmlField.getMapping()!=null && "manytomany".equals(scaffoldXmlField.getMapping().toLowerCase())){
                    sbEdit.append("\r\n").append("            ").append("<td>${" + scaffoldXmlTable.getJavaName() + "." + scaffoldXmlField.getJavaFiledId() + "Names} </td>");
                }else {
                    sbEdit.append("\r\n").append("            ").append("<td>${" + scaffoldXmlTable.getJavaName() + "." + scaffoldXmlField.getJavaFiledId() + "." + scaffoldXmlField.getForeignName() + "} </td>");
                }
            }else{
                sbEdit.append("\r\n").append("            ").append("<td>${"+scaffoldXmlTable.getJavaName()+"."+ scaffoldXmlField.getJavaFiledId()+"} </td>");
            }
            sbTitle.append("\r\n").append("            ").append("<th><fmt:message key=\""+scaffoldXmlTable.getJavaName()+"."+ scaffoldXmlField.getJavaFiledId()+"\"/></th>");
        }
        sbEdit.append("\r\n").append("            ").append("<td>");
        //点开的页面也要配为
        sbEdit.append("\r\n").append("                ").append("<a ui-sref=\""+scaffoldXmlTable.getJavaName()+"Look({id:'${"+scaffoldXmlTable.getJavaName()+".pkId}'})\"><span class=\"font-bold\" ><fmt:message key=\"button.view\"/></span>  </a>");
        sbEdit.append("\r\n").append("                ").append("<a ui-sref=\""+scaffoldXmlTable.getJavaName()+"Edit({id:'${"+scaffoldXmlTable.getJavaName()+".pkId}'})\"><span class=\"font-bold\" ><fmt:message key=\"button.edit\"/></span>  </a>");
        sbEdit.append("\r\n").append("            ").append("</td>");
        sbEdit.append("\r\n").append("     ").append("</tr>");
        srcText=replaceStr(srcText,"${simpleJavaName}",scaffoldXmlTable.getJavaName());
        srcText=replaceStr(srcText,"${listJspCode}",sbEdit.toString());
        srcText=replaceStr(srcText,"${listTiTleCode}",sbTitle.toString());
        srcText=replaceStr(srcText,"${jspCodeTitle}","<fmt:message key=\""+scaffoldXmlTable.getJavaName()+".module\"/>");
        return srcText;
    }
}
